package org.webequa.core.maple.quantity;

import org.apache.log4j.Logger;
import org.webequa.core.common.quantity.ComplexNumber;
import org.webequa.core.common.quantity.QuantityObject;
import org.webequa.core.common.quantity.RealNumber;
import org.webequa.core.common.signature.QuantityObjectSignature;

import com.maplesoft.externalcall.MapleException;
import com.maplesoft.openmaple.Algebraic;

public class MapleQuantityObjectFactory
{
	public static MapleQuantityObject getMapleQuantityObject(QuantityObject in)
	{
		if (in instanceof MapleQuantityObject)
			return (MapleQuantityObject)in;		
		else if (in.getSignature().isRealNumberSignature())
			return new MapleRealNumber(((RealNumber)in).getValue());			
		else if (in.getSignature().isComplexNumberSignature())
			return new MapleComplexNumber(((ComplexNumber)in).getRealPart(),((ComplexNumber)in).getImagePart());
		else if (in.getSignature().isRealMatrixSignature())
			return new MapleRealMatrix();
		else if (in.getSignature().isComplexMatrixSignature())
			return new MapleComplexMatrix();
		else return null;
	}
	
	public static MapleQuantityObject getMapleQuantityObject(QuantityObjectSignature sign, Algebraic op)
	{
		MapleQuantityObject res = null;
		if (sign.isRealNumberSignature())
			res = new MapleRealNumber();
		else if (sign.isComplexNumberSignature())
			res = new MapleComplexNumber();
		else if (sign.isRealMatrixSignature())
			res = new MapleRealMatrix();
		else if (sign.isComplexMatrixSignature())
			res = new MapleComplexMatrix();
		try
		{
			res.getMapleObject().assign(op);
			return res;
		} catch (MapleException e)
		{
			log.error(e);
			return null;
		}			
	}
	private static final Logger log = Logger.getLogger(MapleQuantityObjectFactory.class);
}
